package com.ycsj.goldmedalnewconcept.teacher.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.manager.CustomDialogManager;
import com.ycsj.common.utils.MaxTextLengthFilter;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.bean.ResultInfo;
import com.ycsj.goldmedalnewconcept.common.http.HttpCallBack;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 校长查看老师教学与反思详情界面
 * Created by Administrator on 2019/5/6 0006.
 */

public class TeacherPrepareLessonDetailsActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.tv_num)
    TextView tvNum;

    //反思的内容,单元号,班级号,反思的rowid(更新时使用)
    private String content, unit, classnum;
    private int rowid;

    /**
     * 跳转到当前页面的方法
     *
     * @param context
     * @param content
     */
    public static void goActivity(Context context, String content, String unit, String classnum, int rowid) {
        Intent it = new Intent(context, TeacherPrepareLessonDetailsActivity.class);
        it.putExtra("content", content);
        it.putExtra("unit", unit);
        it.putExtra("classnum", classnum);
        it.putExtra("rowid", rowid);
        context.startActivity(it);
    }


    @Override
    protected int getContentLayout() {
        return R.layout.teacher_activity_look_details;
    }

    @Override
    protected void init() {
        initStateBar();

        //获取内容
        initIntent();

        //设置标题
        initTitle();

        //设置监听
        initListener();

        //设置页面数据
        setPageData();
    }

    /**
     * 设置标题
     */
    private void initTitle() {
        tvTitle.setText("我的思考");
    }

    /**
     * 获取Intent数据
     */
    private void initIntent() {
        content = getIntent().getStringExtra("content");
        unit = getIntent().getStringExtra("unit");
        classnum = getIntent().getStringExtra("classnum");
        rowid = getIntent().getIntExtra("rowid", -1);

        LogUtils.i("test", "页面对象是:" + rowid);
    }

    /**
     * 设置EditText的最大输入字数
     */
    private void initListener() {
        etContent.setFilters(new InputFilter[]{new MaxTextLengthFilter(501)});

        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String inputText = etContent.getText().toString().trim();
                if (TextUtils.isEmpty(inputText)) {
                    tvNum.setText("0/500");
                } else {
                    if (inputText.length() > 501) {
                        tvNum.setText("500/500");
                    } else {
                        tvNum.setText(inputText.length() + "/500");
                    }
                }
            }
        });
    }

    /**
     * 设置页面数据
     */
    private void setPageData() {
        if (!TextUtils.isEmpty(content)) {
            etContent.setText(content);
            if (content.length() > 500) {
                tvNum.setText("500/500");
            } else {
                tvNum.setText(content.length() + "/500");
            }
        } else {
            tvNum.setText("0/500");
        }
    }


    /**
     * 点击事件
     *
     * @param v
     */
    @OnClick({R.id.iv_back, R.id.bt_save})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                //返回
                finish();
                break;
            case R.id.bt_save:
                //保存
                doSaveAction();
                break;

        }
    }

    /**
     * 保存或者更新反思
     */
    private void doSaveAction() {
        final String newContent = etContent.getText().toString().trim();

        if (TextUtils.isEmpty(newContent)) {
            ToastUtils.showShort("无反思内容");
            return;
        }

        int type = -1;

        Map<String, String> map = new HashMap<>();
        if (TextUtils.isEmpty(content)) {
            //没有反思内容,上传新反思
            type = 19;
            map.put("type", type + "");
            map.put("userclass", classnum);
            map.put("unit", unit);
            map.put("lesson", (Integer.parseInt(unit) * 2 - 1) + "");
        } else {
            //有反思内容,更新反思
            if (newContent.equals(content)) {
                ToastUtils.showShort("反思内容没有更新,不需要上传");
                return;
            }
            type = 20;
            map.put("type", type + "");
            map.put("rowid", rowid + "");
            LogUtils.iTag("test", "接受到的rowId是" + rowid);
        }

        map.put("c", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("content", newContent);

        CustomDialogManager.show(this, "保存中...");

//        LogUtils.i("test","type=20&rowid="+rowid+"&c="+AccountUtils.getAccount(this)+"&role="+AccountUtils.getRole(this)+"&content="+newContent);

        final int finalType = type;

        HttpUtils.post(HttpAddress.UPDATAEFANSI, map, this, new HttpCallBack<ResultInfo>() {
            @Override
            public void onSuccess(ResultInfo resultInfo) {

                CustomDialogManager.dissmiss();

                if ("1".equals(resultInfo.getState())) {

                    ToastUtils.showShort("保存成功");

                    //通知单元选择界面进行状态更新
                    BusInfo busInfo = new BusInfo();
                    busInfo.type = TeacherChooseUnitActivity.UPDATESTATE;
                    busInfo.msg = newContent;
                    if (finalType == 19) {
                        //创建反思的时候需要通知前一个界面刷新
                        busInfo.row_id = Integer.parseInt(resultInfo.rowid);
                        busInfo.updateRowId = 110;
                    }

                    EventBus.getDefault().post(busInfo);


                    //关闭当前页面
                    finish();

                } else {
                    ToastUtils.showShort("保存失败,异常码是:" + resultInfo.getState());
                }
            }

            @Override
            public void onError(String e) {
                CustomDialogManager.dissmiss();
                ToastUtils.showShort(BaseApplication.ERROR + ",保存失败");
            }
        });

    }
}
